package com.joysuch.wwyt.bp.controller;


import cn.zlg.common.util.CollectionUtils;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.bean.inspect.BpInspectRecordBean;
import com.joysuch.wwyt.bp.bean.inspect.BpInspectRecordQueryBean;
import com.joysuch.wwyt.bp.entity.inspect.BpInspectRecord;
import com.joysuch.wwyt.bp.service.BpInspectRecordService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/bp/inspect-record")
@Api(tags = "巡检-检查记录")
@CrossOrigin
public class BpInspectRecordController {

    @Autowired
    private BpInspectRecordService bpInspectRecordService;

    @GetMapping("/list")
    @ApiOperation(value = "检查记录列表", response = BpInspectRecord.class)
    public ResultBean list(BpInspectRecordQueryBean bean) {
        Page<BpInspectRecord> list = bpInspectRecordService.list(bean, 0);
        return ResultBean.pageData(list.getContent(), list.getTotalElements());
    }


    @GetMapping("/export")
    @ApiOperation(value = "检查记录列表导出", response = BpInspectRecord.class)
    public void export(BpInspectRecordQueryBean bean, HttpServletRequest request, HttpServletResponse
            response) {
        try {
            bpInspectRecordService.export(bean, 0, request, response);
        } catch (IOException e) {
            log.error("检查记录列表导出",e);
        }
    }

    @PostMapping("/save/{flag}")
    @RequiresPermissions("bp:inspect-record:save")
    @ApiOperation(value = "PC端保存检查记录 & APP端点击开始检查生成即生成检查记录 --注:APP端传值时需要传入任务表id-即BpAppPlanTask")
    @ApiImplicitParam(name = "flag", value = "区分PC端还是APP端 app传1 pc传0", paramType = "path", required = true)
    public ResultBean save(@RequestBody @Valid BpInspectRecordBean bean, @PathVariable Integer flag) {
        return bpInspectRecordService.save(bean, flag);
    }

    @PostMapping("/saveRecordList")
    @RequiresPermissions("bp:inspect-record:save")
    @ApiOperation(value = "app端-批量保存检查记录")
    public ResultBean saveRecordList(@RequestBody @Valid List<BpInspectRecordBean> beanList) {
        List<String> list = Lists.newArrayList();
        for (BpInspectRecordBean bean : beanList) {
            try {
                ResultBean resultBean = bpInspectRecordService.save(bean, 1);
                if (resultBean.getCode() != 0) {
                    list.add(resultBean.getMsg());
                }
            } catch (Exception e) {
                log.error("batch add record error,planId:{},appTaskId:{},error:{}",
                        bean.getInspectPlanId(), bean.getAppPlanTaskId(), e.getMessage());
            }
        }
        List<String> collect = list.stream().distinct().collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(collect)) {
            String join = Joiner.on(",").join(collect);
            return ResultBean.fail(101, join);
        }
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/delete/{id}")
    @RequiresPermissions("bp:inspect-record:delete")
    @ApiOperation(value = "根据检查记录ID删除")
    @ApiImplicitParam(name = "id", value = "检查记录ID", paramType = "path", required = true)
    public ResultBean delete(@PathVariable Long id) {
        bpInspectRecordService.deleteById(id);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据检查记录ID查询(巡检详情)", response = BpInspectRecord.class)
    @ApiImplicitParam(name = "id", value = "检查记录ID", paramType = "path", required = true)
    public ResultBean getById(@PathVariable Long id) {
        return bpInspectRecordService.getById(id);
    }


    @GetMapping("/exceptionHandle/{id}")
    @RequiresPermissions("bp:inspect-record:exceptionHandle")
    @ApiOperation(value = "异常处理")
    @ApiImplicitParam(name = "id", value = "检查记录ID", paramType = "path", required = true)
    public ResultBean exceptionHandle(@PathVariable Long id) {
        return bpInspectRecordService.exceptionHandle(id);
    }

    @GetMapping("/getPlanDetail/{planId}")
    @ApiOperation(value = "根据检查计划id查询检查点详情")
    @ApiImplicitParam(name = "planId", value = "检查计划ID", paramType = "path", required = true)
    public ResultBean getPlanDetailByPlanId(@PathVariable Long planId) {
        return bpInspectRecordService.getPlanDetailByPlanId(planId);
    }

    @GetMapping("/repair/{deviceId}/{flag}")
    @RequiresPermissions("bp:inspect-record:repair")
    @ApiOperation(value = "异常处理-整改 flag: 0 下达整改; 1 整改完成")
    public ResultBean repair(@PathVariable Long deviceId, @PathVariable int flag) {
        return bpInspectRecordService.repair(deviceId, flag);
    }

    @GetMapping("/getRecordDetail")
    @ApiOperation(value = "app 已完成 项目详情查询")
    public ResultBean getRecordDetail(Long inspectRecordId, Long inspectPlanId, Long inspectPointId) {
        return bpInspectRecordService.getRecordDetail(inspectRecordId, inspectPlanId, inspectPointId);
    }

    @GetMapping("/sign-in")
    @ApiOperation("定位方式为'地图'下的签到")
    public ResultBean signIn(Long pointId) {
        return bpInspectRecordService.signIn(pointId);
    }

}
